package com.yueya.common.util;

import java.io.File;
import java.io.IOException;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.MultimediaInfo;

public class MediaUtil {

	// 获取多媒体文件的时长（分钟）
	public static long mediaDurationMin(File file) throws IOException, EncoderException {
		long duration = 0;
		Encoder encoder = new Encoder();
		MultimediaInfo m = encoder.getInfo(file);
		if (m != null) {
			long ls = m.getDuration();
			duration = ls / 60000+1;
			System.out.println("此视频时长为:" + ls / 60000 + "分" + (ls / 1000 - ls / 60000 * 60) + "秒！");
		}

		return duration;
	}

	// 获取多媒体文件的时长（秒钟）
	public static long mediaDurationSec(File file) throws IOException, EncoderException {
		long duration = 0;
		Encoder encoder = new Encoder();
		MultimediaInfo m = encoder.getInfo(file);
		if (m != null) {
			long ls = m.getDuration();
			duration = ls / 60000 * 60;
			System.out.println("此视频时长为:" + ls / 60000 + "分" + (ls / 1000 - ls / 60000 * 60) + "秒！");
		}
		return duration;
	}

	// 获取多媒体文件的总时长（分钟:秒钟）
	public static String mediaDuration(File file) throws IOException, EncoderException {
		String duration = "0";
		Encoder encoder = new Encoder();
		MultimediaInfo m = encoder.getInfo(file);
		if (m != null) {
			long ls = m.getDuration();
			duration = ls / 60000 + ":" + (ls / 1000 - ls / 60000 * 60);
		}
		return duration;
	}

	public static void main(String[] args) {
		File file = new File("d:/test-video2.mp4");
		try {
			System.out.println(mediaDurationMin(file));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (EncoderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
